Skip to content

Fix memory leak in NSAttributedString #5256

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

andriydruk
Copy link
Contributor

Currently, all attributes are leaking in NSAttributedString
This PR adds proper retain/release handling for CFRunArray in the appropriate places.

A test was added to validate that attribute values in NSAttributedString are no longer leaking.

@@ -200,6 +200,14 @@ CFRunArrayRef CFRunArrayCreate(CFAllocatorRef allocator) {
return array;
}

CFRunArrayRef CFRunArrayRetain(CFRunArrayRef array) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is CFRunArray not a regular CF type? (CFRetain, CFRelease should work)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The COPY below is just a typedef to CFRetain:

#define COPY(obj) CFRetain(obj)

So yeah I think we can just call CFRetain/CFRelease from NSAttributedString.swift (unless there's an issue with how it's imported by Swift?) and that should be fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I try to use CFRetain, I get the following error:

'CFRetain' is unavailable: Core Foundation objects are automatically memory managed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if it’s automatically memory managed, where is the leak coming from? We don’t have to do this with other CF types.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My guess is that this is simply not true for non-Darwin platforms.
I’m not sure how this "automatically memory managed" behavior is supposed to work there.
Are there any tests for this?

@parkera parkera requested a review from jmschonfeld August 4, 2025 22:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants